package soso.Utils;

import soso.Dao.*;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author ：lzc
 * @date ：Created in 2021/8/20 0020 13:10
 * @description：
 */

public class CardsUtil {
    //资费txt
    static String str = "src\\soso\\File\\zf.txt";
    //用户txt
    static String path = "src\\soso\\File\\Userfile.txt";
    static HashMap<String, User> userlist = new HashMap<>();
    //消费txt
    static String path2 = "src\\soso\\File\\consumInfos.txt";
    static HashMap<String, ArrayList<String>> conlist = new HashMap<>();
    //短信txt
    static String path3 = "src\\soso\\File\\Goodsfile.txt";
    static HashMap<String, ArrayList<String>> messagelist = new HashMap<>();
    static DecimalFormat f = new DecimalFormat("0.00");
    static Scanner sc = new Scanner(System.in);

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        initCard();
    }

    //一级菜单
    public static void initCard() throws IOException, ClassNotFoundException {
        while (true) {
            while (true) {
                try {
                    System.out.println("********************sousou 5G 移动大厅********************");
                    System.out.println("1.登入");
                    System.out.println("2.注册");
                    System.out.println("3.退出");
                    switch (sc.nextInt()) {
                        case 1:
                            String x = login();
                            if (x != null) {
                                two(x);
                            }
                            ;
                            break;
                        case 2:
                            res();
                            break;
                        case 3:
                            System.exit(0);
                            break;
                        default:
                            System.out.println("请选择有效选项");
                    }
                } catch (InputMismatchException e) {
                    System.out.println("[ " + sc.next() + " ]不是合法的整数\n请重新输入：");
                }
            }
        }
    }

    //判断是否扣月租
    public static boolean MM(String id) throws IOException, ClassNotFoundException {
        readFile();
        conreadFile();//读消费记录
        Calendar calendar = Calendar.getInstance();
        if (calendar.after(userlist.get(id).getPhonecard().getCoutime())) {
            Taojilv(id,
                    userlist.get(id).getPhonecard().getTaocan().toString(),
                    userlist.get(id).getPhonecard().getTaocan().getMonfee());
            calendar.add(Calendar.SECOND, 30);
            userlist.get(id).getPhonecard().setCoutime(calendar);
            userlist.get(id).getPhonecard().setMoney(
                    userlist.get(id).getPhonecard().getMoney() - userlist.get(id).getPhonecard().getTaocan().getMonfee());
            if (userlist.get(id).getPhonecard().getTaocan() instanceof TalkPackage) {
                userlist.get(id).getPhonecard().setTaocan(new TalkPackage());
                userlist.get(id).getPhonecard().getTaocan().setMonfee(58);
            } else if (userlist.get(id).getPhonecard().getTaocan() instanceof SuperPackage) {
                userlist.get(id).getPhonecard().setTaocan(new SuperPackage());
                userlist.get(id).getPhonecard().getTaocan().setMonfee(78);
            } else {
                userlist.get(id).getPhonecard().setTaocan(new NetPackage());
                userlist.get(id).getPhonecard().getTaocan().setMonfee(68);
            }
            userlist.get(id).getPhonecard().setMonthlyfee(0);
            userlist.get(id).getPhonecard().setRecharge(0);
            writer(userlist);
            return true;
        }
        return false;
    }

    //欠费提醒充值
    public static void TChonzhi(String id) throws IOException, ClassNotFoundException {
        readFile();
        if (userlist.get(id).getPhonecard().getMoney() < 0) {
            System.out.println("你已欠费" + f.format(userlist.get(id).getPhonecard().getMoney()) + "元,请尽快充值!");
        }
    }

    //使用sousou二级菜单
    public static void two(String id) throws IOException, ClassNotFoundException {
        boolean b = MM(id);     //判断是否扣月租,每次登入判断一次,30秒/一扣
        caca:
        while (true) {
            readFile();
            User u = userlist.get(id);
            System.out.println("********************使用sousou********************");
            System.out.println("当前登入用户为:" + u.getUsername());
            System.out.println("用户卡号为:" + u.getPhonecard().getPhonenum());
            System.out.println("用户当前套餐为:" + u.getPhonecard().getTaocan().toString());
            System.out.println("当前余额为:" + f.format(u.getPhonecard().getMoney()));
            if (b) {
                b = false;
                System.out.println("本月套餐费以扣除");
            }
            TChonzhi(id);
            if (u.getPhonecard().getBo()) {
                System.out.println(userlist.get(id).getPhonecard().getPhonenum() + "打电话给你");
                System.out.println("是否接听?(输入任意接通,输入0退出)");
                if (!sc.next().equals("0")) {
                    userlist.get(id).getPhonecard().setBo(false);
                    new Cli(id, userlist.get(id).getPhonecard().getCallid());
                    System.out.println("通话结束");
                    writer(userlist);
                } else {
                    userlist.get(id).getPhonecard().setBo(false);
                    writer(userlist);
                }

            }
            //三级菜单
            System.out.println("1.话费充值");
            System.out.println("2.账单管理");
            System.out.println("3.套餐管理");
            System.out.println("4.上网");
            System.out.println("5.办理退网");
            System.out.println("6.返回上一级");
            try {
                switch (sc.nextInt()) {
                    case 1:
                        Recharge(id);   //话费
                        ;
                        break;
                    case 2:
                        Bill(id);   //账单
                        ;
                        break;
                    case 3:
                        ChangeMe(id);   //套餐
                        break;
                    case 4:
                        online(id);     //上网
                        ;
                        break;
                    case 5:
                        if (ExitCard(id)) {
                            break caca;
                        } else {
                            break;
                        }
                    case 6:
                        break caca;
                    default:
                        System.out.println("请输入正确选项!");
                }
            } catch (InputMismatchException e) {
                System.out.println("[ " + sc.next() + " ]不是合法的整数\n请重新输入：");
            }
        }

    }

    //短信读
    public static void Mereader() throws IOException {
        try {
            ObjectInputStream r = new ObjectInputStream(new BufferedInputStream(new FileInputStream(path3)));
            messagelist = (HashMap<String, ArrayList<String>>) r.readObject();
            r.close();
        } catch (EOFException | ClassNotFoundException e) {

        } catch (NullPointerException e) {
            System.out.println("当前无短信接收");
        }
    }

    //短信写
    public static void Mewriter() throws IOException {
        ObjectOutputStream w = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(path3)));
        w.writeObject(messagelist);
        w.flush();
        w.close();
    }

    //添加短信记录
    public static void AddMess(String id) throws IOException, ClassNotFoundException {
        readFile();
        conreadFile();
        if (userlist.get(id).getPhonecard().getTaocan().getMessagesnum() <= 0) {
            userlist.get(id).getPhonecard().setMoney(userlist.get(id).getPhonecard().getMoney() - 0.2f);
            Taojilv(id, "超出套餐发送一条短信", 0.2f);
            userlist.get(id).getPhonecard().setMonthlyfee(userlist.get(id).getPhonecard().getMonthlyfee() + 0.2f);

            writer(userlist);
        } else {
            userlist.get(id).getPhonecard().getTaocan().setMessagesnum(
                    userlist.get(id).getPhonecard().getTaocan().getMessagesnum() - 1);
            writer(userlist);

        }
    }

    //写信
    public static void wMess(String id) throws IOException, ClassNotFoundException {
        while (true) {
            readFile();
            System.out.println("**********************写信**********************");
            System.out.println("请输入接收人卡号(输入0退出):");
            String s1 = sc.next();//
            if (s1.equals("0")) {
                break;
            }
            if (s1.equals(id)) {
                System.out.println("不能给自己发短信");
                break;
            }
            if (userlist.containsKey(s1)) {
                System.out.print("请输入要发送的内容:");
                String s2 = sc.next();
                try {
                    Mereader();
                    ArrayList<String> list = messagelist.get(s1);
                    list.add(getDate() + " " + id + " 发送一条信息 " + " 内容: " + s2);
                    System.out.println("发送成功!");
                    messagelist.put(s1, list);
                    AddMess(id);
                    Mewriter();

                } catch (NullPointerException e) {
                    ArrayList<String> list = new ArrayList<>();
                    list.add(getDate() + " " + id + " 发送一条信息 " + " 内容: " + s2);
                    System.out.println("发送成功!");
                    AddMess(id);
                    messagelist.put(s1, list);
                    Mewriter();
                }
                System.out.println("任意键继续,0退出");
                if (sc.next().equals("0")) {
                    break;
                } else {
                    continue;
                }
            } else {
                System.out.println("该卡号不存在!请重新输入");
                continue;
            }

        }
    }

    //收信
    public static void Receive(String id) throws IOException {
        Mereader();
        try {
            System.out.println("*********************收件箱*********************");
            ArrayList<String> list = messagelist.get(id);
            for (String s : list
            ) {
                System.out.println(s);
            }
        } catch (NullPointerException e) {
            System.out.println("无短信");
        }

    }

    //短信服务
    public static void MeServer(String id) {
        bba:
        while (true) {
            System.out.println("********************短信服务********************");
            System.out.println("1.写信");
            System.out.println("2.收件箱");
            System.out.println("3.清空收件箱");
            System.out.println("4.返回上一级");
            try {
                switch (sc.nextInt()) {
                    case 1:
                        wMess(id);
                        ;   //写信
                        break;
                    case 2:
                        Receive(id)
                        ;   //收件箱
                        break;
                    case 3:
                        ClearMsg(id);
                        ;        //返回上一级
                        break;
                    case 4:
                        ;        //返回上一级
                        break bba;
                    default:
                        System.out.println("请输入正确选项!");
                }
            } catch (InputMismatchException e) {
                System.out.println("[ " + sc.next() + " ]不是合法的整数\n请重新输入：");
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
    }

    //清空收件箱
    private static void ClearMsg(String id) throws IOException {
        System.out.println("输入0删除,任意键退出");
        if (sc.next().equals("0")) {
            try {
                Mereader();
                messagelist.remove(id);
                System.out.println("删除成功!");
                Mewriter();
            } catch (NullPointerException e) {
                System.out.println("当前无短信删除!");
            }

        }
    }

    //打电话
    public static void CheatServer(String id) throws IOException, ClassNotFoundException {
        long a = System.currentTimeMillis();
        while (true) {
            readFile();
            System.out.println("请拨打号码:");
            String id2 = sc.next();
            if (userlist.containsKey(id2) && id2 != id) {
                userlist.get(id2).getPhonecard().setCallid(id);
                userlist.get(id2).getPhonecard().setBo(true);
                writer(userlist);
                System.out.println("等待 " + id2 + "接通....");
                new ServerDemo(id, id2);
                System.out.println("通话结束!");
                userlist.get(id2).getPhonecard().setBo(false);
                break;
            } else {
                System.out.println("您拨打是空号!请重新拨打");
                System.out.println("输入任意键继续,0退出");
                if (sc.next().equals("0")) {
                    break;
                }
                continue;
            }
        }
        long b = System.currentTimeMillis();
        //得到通话时间
        calltime(a, b, id);
    }

    //通话时间
    public static void calltime(long a, long b, String id) throws IOException, ClassNotFoundException {
        int x = (int) Math.ceil((b - a) / 1000f / 60);
        System.out.println("本次通话时间为" + x + "分钟");
        readFile();
        if (userlist.get(id).getPhonecard().getTaocan().getCalltime() - x <= 0) {
            //超出通话时间
            int y = x - userlist.get(id).getPhonecard().getTaocan().getCalltime();
            userlist.get(id).getPhonecard().setMoney(
                    userlist.get(id).getPhonecard().getMoney() - (float) (y * 0.2));
            userlist.get(id).getPhonecard().setMonthlyfee(userlist.get(id).getPhonecard().getMonthlyfee() + (float) (y * 0.2));
            userlist.get(id).getPhonecard().getTaocan().setCalltime(0);
            Taojilv(id, "套餐外超出通话", (float) (y * 0.2));
            writer(userlist);
        } else {
            userlist.get(id).getPhonecard().getTaocan().setCalltime(
                    userlist.get(id).getPhonecard().getTaocan().getCalltime() - x);
            writer(userlist);
        }
    }

    //上网
    public static void online(String id) throws IOException, ClassNotFoundException {
        long a = System.currentTimeMillis();
        ccc:
        while (true) {
            System.out.println("**********************上网**********************");
            System.out.println("1.打电话");
            System.out.println("2.短信服务");
            System.out.println("3.返回上一级");
            try {
                switch (sc.nextInt()) {
                    case 1:
                        CheatServer(id);
                        ;   //在线聊天
                        break;
                    case 2:
                        MeServer(id);
                        ;   //短信服务
                        break;
                    case 3:
                        ;        //返回上一级
                        break ccc;
                    default:
                        System.out.println("请输入正确选项!");
                }
            } catch (InputMismatchException | IOException | ClassNotFoundException e) {
                System.out.println("[ " + sc.next() + " ]不是合法的整数\n请重新输入：");
            }
        }
        long b = System.currentTimeMillis();
        //计算时间
        onlinetime(a, b, id);
    }

    //计算上网时间
    public static void onlinetime(long a, long b, String id) throws IOException, ClassNotFoundException {
        int x = (int) Math.ceil((b - a) / 1000f / 60);
        System.out.println("本次上网" + x + "分钟");

        readFile();
        if (userlist.get(id).getPhonecard().getTaocan().getFlow() - x <= 0) {
            //超出流量
            int y = x - userlist.get(id).getPhonecard().getTaocan().getFlow();
            double c = (float) y * 0.1;
            userlist.get(id).getPhonecard().setMoney(
                    userlist.get(id).getPhonecard().getMoney() - (float) c);
            userlist.get(id).getPhonecard().setMonthlyfee(userlist.get(id).getPhonecard().getMonthlyfee() + (float) c);

            Taojilv(id, "套餐外超出流量", (float) c);
            userlist.get(id).getPhonecard().getTaocan().setFlow(0);
            writer(userlist);
        } else {
            userlist.get(id).getPhonecard().getTaocan().setFlow(
                    userlist.get(id).getPhonecard().getTaocan().getFlow() - x);
            writer(userlist);
        }

    }

    //退网
    public static boolean ExitCard(String id) throws IOException, ClassNotFoundException {
        readFile();
        User u = userlist.get(id);
        System.out.println("********************退网********************");
        System.out.println("当前登入用户为:" + u.getUsername());
        System.out.println("用户卡号为:" + u.getPhonecard().getPhonenum());
        System.out.println("用户当前套餐为:" + u.getPhonecard().getTaocan().toString());
        System.out.println("当前余额为:" + u.getPhonecard().getMoney());
        if (u.getPhonecard().getMoney() < 0) {
            System.out.println("您当前欠费" + u.getPhonecard().getMoney() + "元,不可退网");
            System.out.println("请先充值!");
            return false;
        }
        pp:
        while (true) {
            try {
                System.out.println("是否确定退网? 确认请输入1,退出输入0");
                switch (sc.nextInt()) {
                    case 0:
                        ;
                        break pp;
                    case 1:
                        userlist.remove(id);
                        System.out.println("退网成功!");
                        writer(userlist);
                        return true;
                    default:
                        System.out.println("请输入正确序号!");
                }
            } catch (InputMismatchException e) {
                System.out.println("[ " + sc.next() + " ]不是合法的整数\n请重新输入：");
            }
        }
        return false;
    }

    //套餐变更三级菜单
    public static void ChangeMe(String id) {
        oof:
        while (true) {
            try {
                System.out.println("********************套餐办理********************");
                System.out.println("1.套餐余量查询");
                System.out.println("2.套餐变更");
                System.out.println("3.套餐资费说明");
                System.out.println("4.返回上一级");
                switch (sc.nextInt()) {
                    case 1:
                        ChaTaoCan(id)
                        ;
                        break;
                    case 2:
                        ChangeTaoCan(id)
                        ;
                        break;
                    case 3:
                        ;
                        TariffShow();
                        break;
                    case 4:
                        ;
                        break oof;
                    default:
                        System.out.println("请输入正确选项!");
                }
            } catch (InputMismatchException e) {
                System.out.println("[ " + sc.next() + " ]不是合法的整数\n请重新输入：");
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
    }

    //套餐变更记录添加
    public static void Taojilv(String id, String x, float y) throws IOException, ClassNotFoundException {
        conreadFile();
        try {
            ArrayList<String> list = conlist.get(id);
            list.add(getDate() + "  " + userlist.get(id).getUsername() + "  " + x + "  " + y + "元");
            conlist.put(id, list);
            conwriter();
        } catch (NullPointerException e) {
            ArrayList<String> list = new ArrayList<>();
            list.add(getDate() + "  " + userlist.get(id).getUsername() + "  " + x + "  " + y + "元");
            conlist.put(id, list);
            conwriter();
        }
    }

    //套餐变更
    public static boolean ChangeTaoCan(String id) throws IOException, ClassNotFoundException {
        readFile();//读取最新数据
        conreadFile();
        User u = userlist.get(id);
        System.out.println("当前套餐为:" + u.getPhonecard().getTaocan().toString());//显示当前套餐
        System.out.println("当前可换套餐有:");
        if (u.getPhonecard().getTaocan() instanceof TalkPackage && u.getPhonecard().getMoney() >= 20) {
            System.out.print("1.超人套餐78元/月" + " ");
            System.out.println("2.网虫套餐68元/月");
            System.out.println("3.退出");
            System.out.print("请选择:");
            while (true) {
                try {
                    switch (sc.nextInt()) {
                        case 1:
                            u.getPhonecard().setTaocan(new SuperPackage());
                            u.getPhonecard().setMoney(u.getPhonecard().getMoney() - 20);

                            u.getPhonecard().getTaocan().setMonfee(78);
                            Taojilv(id, "变更超人套餐扣除", -20);
                            System.out.println("更改成功!");
                            ;
                            break;
                        case 2:
                            u.getPhonecard().setTaocan(new NetPackage());
                            u.getPhonecard().setMoney(u.getPhonecard().getMoney() - 10);


                            u.getPhonecard().getTaocan().setMonfee(68);
                            Taojilv(id, "变更网虫套餐扣除", -10);
                            System.out.println("更改成功!");
                            ;
                            break;
                        case 3:
                            ;
                            break;
                        default:
                            System.out.println("请输入有效序号!");
                    }
                    break;
                } catch (InputMismatchException e) {
                    System.out.println("[ " + sc.next() + " ]不是合法的整数\n请重新输入：");
                }
            }
        } else if (u.getPhonecard().getTaocan() instanceof TalkPackage && u.getPhonecard().getMoney() >= 10) {
            System.out.println("1.网虫套餐68元/月");
            System.out.println("2.退出");
            System.out.print("请选择:");
            while (true) {
                try {
                    switch (sc.nextInt()) {
                        case 1:

                            u.getPhonecard().setTaocan(new NetPackage());
                            u.getPhonecard().setMoney(u.getPhonecard().getMoney() - 10);
                            u.getPhonecard().getTaocan().setMonfee(68);
                            Taojilv(id, "变更网虫套餐扣除", -10);
                            System.out.println("更改成功!");
                            ;
                            break;
                        case 2:
                            ;
                            break;
                        default:
                            System.out.println("请输入有效序号!");
                    }
                    break;
                } catch (InputMismatchException e) {
                    System.out.println("[ " + sc.next() + " ]不是合法的整数\n请重新输入：");
                }
            }
        } else if (u.getPhonecard().getTaocan() instanceof NetPackage && u.getPhonecard().getMoney() >= 10) {
            System.out.print("1.超人套餐78元/月" + " ");
            System.out.println("2.话痨套餐58元/月");
            System.out.println("3.退出");
            System.out.print("请选择:");
            while (true) {
                try {
                    switch (sc.nextInt()) {
                        case 1:
                            u.getPhonecard().setTaocan(new SuperPackage());
                            u.getPhonecard().setMoney(u.getPhonecard().getMoney() - 10);
                            u.getPhonecard().getTaocan().setMonfee(78);
                            Taojilv(id, "变更超人套餐扣除", -10);
                            System.out.println("更改成功!");
                            ;
                            break;
                        case 2:
                            u.getPhonecard().setTaocan(new TalkPackage());
                            u.getPhonecard().setMoney(u.getPhonecard().getMoney() + 10);
                            u.getPhonecard().getTaocan().setMonfee(58);
                            Taojilv(id, "变更话痨套餐退还", 10);
                            System.out.println("更改成功!");
                            ;
                            break;
                        case 3:

                            ;
                            break;
                        default:
                            System.out.println("请输入有效序号!");
                    }
                    break;
                } catch (InputMismatchException e) {
                    System.out.println("[ " + sc.next() + " ]不是合法的整数\n请重新输入：");
                }
            }

        } else if (u.getPhonecard().getTaocan() instanceof NetPackage && u.getPhonecard().getMoney() < 10) {
            System.out.println("1.话痨套餐58元/月");
            System.out.println("2.退出");
            System.out.print("请选择:");
            while (true) {
                try {
                    switch (sc.nextInt()) {
                        case 1:
                            u.getPhonecard().setTaocan(new TalkPackage());
                            u.getPhonecard().setMoney(u.getPhonecard().getMoney() + 10);
                            u.getPhonecard().getTaocan().setMonfee(58);
                            Taojilv(id, "变更话痨套餐退还", 10);
                            System.out.println("更改成功!");
                            ;
                            break;
                        case 2:

                            ;
                            break;
                        default:
                            System.out.println("请输入有效序号!");
                    }
                    break;
                } catch (InputMismatchException e) {
                    System.out.println("[ " + sc.next() + " ]不是合法的整数\n请重新输入：");
                }
            }

        } else if (u.getPhonecard().getTaocan() instanceof SuperPackage) {
            System.out.print("1.话痨套餐58元/月" + " ");
            System.out.println("2.网虫套餐68元/月");
            System.out.print("请选择:");
            while (true) {
                try {
                    switch (sc.nextInt()) {
                        case 1:
                            u.getPhonecard().setTaocan(new TalkPackage());
                            u.getPhonecard().setMoney(u.getPhonecard().getMoney() + 20);
                            u.getPhonecard().getTaocan().setMonfee(58);
                            Taojilv(id, "变更话痨套餐退还", 20);
                            System.out.println("更改成功!");
                            ;
                            break;
                        case 2:
                            u.getPhonecard().setTaocan(new NetPackage());
                            u.getPhonecard().setMoney(u.getPhonecard().getMoney() + 10);
                            u.getPhonecard().getTaocan().setMonfee(68);
                            Taojilv(id, "变更网虫套餐退还", 10);
                            System.out.println("更改成功!");
                            ;
                            break;
                        default:
                            System.out.println("请输入有效序号!");
                    }
                    break;
                } catch (InputMismatchException e) {
                    System.out.println("[ " + sc.next() + " ]不是合法的整数\n请重新输入：");
                }
            }
        } else {
            System.out.println("当前余额不足,无法更换套餐");
            return false;
        }
        writer(u);
        System.out.println("当前套餐为:" + u.getPhonecard().getTaocan().toString());//显示当前套餐
        return true;
    }

    //套餐余量查询
    public static void ChaTaoCan(String id) throws IOException, ClassNotFoundException {
        readFile();
        userlist.get(id).getPhonecard().getTaocan().Taocanyuelian();
    }

    //得到时间
    public static String getDate() {
        SimpleDateFormat s = new SimpleDateFormat(" yyyy-MM-dd HH:mm:ss ");
        Calendar c = Calendar.getInstance();
        Date date = c.getTime();
        return s.format(date);
    }

    //得到扣费时间
    public static Calendar CouDate() {
        SimpleDateFormat s = new SimpleDateFormat(" yyyy-MM-dd HH:mm:ss ");
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MINUTE, 5);
        return c;
    }

    //得到创建时间
    public static Calendar CreatDate() {
        SimpleDateFormat s = new SimpleDateFormat(" yyyy-MM-dd HH:mm:ss ");
        Calendar c = Calendar.getInstance();
        return c;
    }

    //账单管理
    public static void Bill(String id) throws IOException, ClassNotFoundException {
        bbc:
        while (true) {
            try {
                readFile();
                System.out.println("**********************账单**********************");
                System.out.println("1.本月账单查询");
                System.out.println("2.消费记录打印");
                System.out.println("3.退出");
                switch (sc.nextInt()) {
                    case 1:
                        Monzhan(id);    //本月账单
                        ;
                        break;
                    case 2:
                        catMoney(id)    //打印消费记录
                        ;
                        break;
                    case 3:
                        ;
                        break bbc;      //返回上一级
                    default:
                        System.out.println("请输入正确选项!");
                }
            } catch (InputMismatchException e) {
                System.out.println("[ " + sc.next() + " ]不是合法的整数\n请重新输入：");
            }
        }
    }

    //本月账单
    public static void Monzhan(String id) throws IOException, ClassNotFoundException {
        readFile();
        System.out.println("********************本月账单********************");
        System.out.println("卡号:" + userlist.get(id).getPhonecard().getPhonenum());
        System.out.println("当前余额:" + f.format(userlist.get(id).getPhonecard().getMoney()));
        System.out.println("套餐基本消费为:" + f.format(userlist.get(id).getPhonecard().getTaocan().getMonfee()) + "元");
        System.out.println("本月一共消费" + f.format((userlist.get(id).getPhonecard().getMonthlyfee() + userlist.get(id).getPhonecard().getTaocan().getMonfee())) + "元");
        System.out.println("本月一共充值" + f.format(userlist.get(id).getPhonecard().getRecharge()) + "元");
    }

    //打印消费记录
    public static void catMoney(String id) throws IOException, ClassNotFoundException {
        conreadFile();
        System.out.println("********************消费详情********************");
        try {
            for (String s : conlist.get(id)
            ) {
                System.out.println(s);
            }
        } catch (NullPointerException e) {
            System.out.println("暂无消费记录!");
        }

    }

    // 充值
    public static void Recharge(String id) throws IOException, ClassNotFoundException {
        System.out.println("********************充值********************");
        float f = 0;
        while (true) {
            System.out.print("请输入要充值的金额:");
            f = sc.nextFloat();
            if (f <= 0) {
                System.out.println("充值金额不能为0,或负数");
            } else {
                break;
            }
        }
        userlist.get(id).getPhonecard().setRecharge(userlist.get(id).getPhonecard().getRecharge() + f);
        userlist.get(id).getPhonecard().setMoney(userlist.get(id).getPhonecard().getMoney() + f);
        addCon(id, f, userlist.get(id).getUsername());
        System.out.println("充值成功!");
        System.out.println("当前余额为:" + userlist.get(id).getPhonecard().getMoney());
        writer(userlist.get(id));
    }

    //充值记录添加
    public static void addCon(String id, float f, String name) throws IOException, ClassNotFoundException {
        conreadFile();
        try {
            ArrayList<String> list = conlist.get(id);
            list.add(getDate() + "  " + name + " 充值 " + "+" + f + "元");
            conlist.put(id, list);
            conwriter();
        } catch (NullPointerException e) {
            ArrayList<String> list = new ArrayList<>();
            list.add(getDate() + "  " + name + " 充值 " + "+" + f + "元");
            conlist.put(id, list);
            conwriter();
        }

    }

    //登录
    public static String login() throws IOException, ClassNotFoundException {
        boolean b = true;
        while (b) {
            readFile();
            System.out.println("********************登录********************");
            User user = new User();
            System.out.print("请输入用户名:");
            String name = sc.next();
            user.setUsername(name);
            System.out.print("请输入密码");
            user.setPassword(sc.next());
            Iterator it = userlist.keySet().iterator();
            while (it.hasNext()) {
                String x = (String) it.next();
                if (userlist.get(x).getUsername().equals(user.getUsername())) {
                    if (userlist.get(x).getPassword().equals(user.getPassword())) {
                        return x;
                    } else {
                        System.out.println("密码错误");
                        System.out.println("输入任意键放回登录,输入0退出");
                        if (sc.next().equals("0")) {
                            b = false;
                        }
                    }
                } else {
                    System.out.println("用户名不存在!");
                    System.out.println("输入任意键放回登录,输入0退出");
                    if (sc.next().equals("0")) {
                        b = false;
                    }
                }

            }


        }
        return null;
    }

    //注册
    public static void res() throws IOException, ClassNotFoundException {
        while (true) {
            User user = new User();
            System.out.println("**********************注册**********************");
            PhoneCard phoneCard = new PhoneCard();
            System.out.print("请输入要注册的用户名");
            user.setUsername(sc.next());//得到用户名
            readFile();
            if (findname(user.getUsername())) {
                System.out.println("该用户名已存在!请重新注册");
                continue;
            }
            System.out.println("********************选择卡号********************");
            HashMap<Integer, String> hs = getCard();
            int p = 1;
            while (true) {
                while (true) {
                    try {
                        System.out.println("请输入要选的卡号(输入对应的序号)");
                        p = sc.nextInt();
                        break;
                    } catch (InputMismatchException e) {
                        System.out.println("[ " + sc.next() + " ] 不是合法的整数\n请重新输入：");
                    }
                }
                if (p > 0 && p <= 9) {
                    phoneCard.setPhonenum(hs.get(p));//获得卡号
                    user.setPhonecard(phoneCard);
                    break;
                } else {
                    System.out.println("请输入正确的序号");
                    continue;
                }
            }

            readFile();
            if (findname(phoneCard.getPhonenum())) {
                System.out.println("该卡号已存在!请重新注册");
                continue;
            }
            System.out.println("请输入要注册的密码"); //得到密码
            user.setPassword(sc.next());
            while (true) {
                try {
                    System.out.println("请选择预存金额(最低58元)");
                    float x = sc.nextFloat();
                    user.getPhonecard().setMoney(x);
                    if (user.getPhonecard().getMoney() < 58) {
                        System.out.println("不可少于58元,请重新输入!");
                        continue;
                    }
                    user.getPhonecard().setRecharge(x);//月充值
                    System.out.println("充值成功!");
                    System.out.println("请选择充值基础套餐");
                    System.out.println("1.话痨套餐58元/月");
                    System.out.println("2.网虫套餐68元/月");
                    System.out.println("3.超人套餐78元/月");
                    switch (sc.nextInt()) {
                        case 1:
                            phoneCard.setTaocan(new TalkPackage());
                            phoneCard.getTaocan().setMonfee(58);
                            phoneCard.setMoney(user.getPhonecard().getMoney() - 58);
                            Initjilv(phoneCard.getPhonenum(), user.getUsername(), "选择话痨套餐", 58);
                            ;
                            break;  //话痨套餐
                        case 2:
                            phoneCard.setTaocan(new NetPackage());
                            phoneCard.getTaocan().setMonfee(68);
                            phoneCard.setMoney(user.getPhonecard().getMoney() - 68);
                            Initjilv(phoneCard.getPhonenum(), user.getUsername(), "选择网虫套餐", 68);
                            ;
                            break;   //网虫套餐
                        case 3:
                            phoneCard.setTaocan(new SuperPackage());
                            phoneCard.getTaocan().setMonfee(78);
                            phoneCard.setMoney(user.getPhonecard().getMoney() - 78);
                            Initjilv(phoneCard.getPhonenum(), user.getUsername(), "选择超人套餐", 78);
                            ;
                            break;  //超人套餐
                    }

                } catch (Exception e) {
                    System.out.println("[ " + sc.next() + " ] 不是合法的整数\n请重新输入：");
                }
                break;
            }
            user.getPhonecard().setCreatime(CreatDate());
            user.getPhonecard().setCoutime(CouDate());
            user.getPhonecard().setMonthlyfee(user.getPhonecard().getTaocan().Billing());
            user.showAll();
            System.out.println("注册成功!");
            writer(user);
            break;
        }
    }

    //写
    public static void writer(User u) throws IOException, ClassNotFoundException {
        readFile();
        userlist.put(u.getPhonecard().getPhonenum(), u);
        ObjectOutputStream w = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(path)));
        w.writeObject(userlist);    //写入文件
        w.flush();
        w.close();
    }

    //写
    public static void writer(HashMap<String, User> userli) throws IOException, ClassNotFoundException {
        ObjectOutputStream w = new ObjectOutputStream(
                new BufferedOutputStream(new FileOutputStream(path)));
        w.writeObject(userli);    //写入文件
        w.flush();
        w.close();
    }

    //读操作
    public static void readFile() throws IOException, ClassNotFoundException {
        try {
            ObjectInputStream r = new ObjectInputStream(
                    new BufferedInputStream(new FileInputStream(path)));
            userlist = (HashMap<String, User>) r.readObject();
            r.close();
        } catch (EOFException e) {

        }
    }

    //消费写
    public static void conwriter() throws IOException, ClassNotFoundException {
        ObjectOutputStream w = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(path2)));
        w.writeObject(conlist);    //写入文件
        w.flush();
        w.close();
    }

    //消费读
    public static void conreadFile() throws IOException, ClassNotFoundException {
        try {
            ObjectInputStream r = new ObjectInputStream(new BufferedInputStream(new FileInputStream(path2)));
            conlist = (HashMap<String, ArrayList<String>>) r.readObject();
            r.close();
        } catch (EOFException e) {

        }
    }


    //初始化套餐记录
    public static void Initjilv(String id, String x, String y, float z) throws IOException, ClassNotFoundException {
        readFile();
        conreadFile();
        ArrayList<String> list = new ArrayList<>();
        list.add(getDate() + "  " + x + " " + y + " " + z + "元");
        conlist.put(id, list);
        conwriter();
    }

    //判断重复
    public static boolean findcard(String id) throws IOException, ClassNotFoundException {
        readFile();
        Iterator it = userlist.keySet().iterator();
        while (it.hasNext()) {
            if (userlist.containsKey(id)) {
                return true;
            }
        }
        return false;
    }

    public static boolean findname(String name) throws IOException, ClassNotFoundException {
        Iterator it = userlist.keySet().iterator();
        while (it.hasNext()) {
            if (userlist.get(it.next()).getUsername().equals(name)) {
                return true;
            }
        }
        return false;
    }

    //得到注册卡号
    public static HashMap<Integer, String> getCard() {
        HashMap<Integer, String> hs = new HashMap<>();
        DecimalFormat df = new DecimalFormat("0");
        for (int i = 1; i <= 9; i++) {
            String s = new String("13");
            s += (df.format((Math.random() + 9) * 1000000000));
            if (i % 3 == 0) {
                hs.put(i, s);
                System.out.println("卡" + i + "  " + s + " ");
            } else {
                hs.put(i, s);
                System.out.print("卡" + i + "  " + s + " ");
            }
        }
        return hs;
    }

    //资费说明
    public static void TariffShow() throws IOException {

        InputStreamReader r = new InputStreamReader(new FileInputStream(str), "UTF-8");
        BufferedReader io = new BufferedReader(r);
        String s;
        while ((s = io.readLine()) != null) {
            System.out.println(s);
        }
        io.close();
    }

    //打电话
    static class ServerDemo extends Thread {
        String id;
        String id2;

        public ServerDemo(String id, String id2) throws IOException {
            this.id = id;
            this.id2 = id2;
            //创建服务器端 socket 并设定端口
            ServerSocket s = new ServerSocket(8887);
            //监听并等待客户端请求，返回客户端
            Socket s1 = s.accept();
            System.out.println(id2 + "接通了");
            DataInputStream di = new DataInputStream(s1.getInputStream());
            OutputStream ou = s1.getOutputStream();
            DataOutputStream dt = new DataOutputStream(ou);
            //要发出信息，先从控制台敲内容
            Scanner sc = new Scanner(System.in);
            String info;//显示在控制台
            while (true) {//一直聊
                //先把客户端的信息读出来
                info = di.readUTF();
                System.out.println(id2 + "说" + info);
                if (info.equals("bye")) {//如果对方说了bye,就结束
                    break;
                }
                System.out.print("我说:");
                String info2 = sc.next();//我打字说
                dt.writeUTF(info2);
                if (info2.equals("bye")) {
                    break;
                }
            }
            dt.close();
            di.close();
        }
    }

    //接电话
    static class Cli {
        String id;
        String id2;

        public Cli(String id, String id2) throws IOException, ClassNotFoundException {
            this.id = id;
            this.id2 = id2;
            //先连接本地服务器，通过端口
            Socket so = new Socket("localhost", 8887);
            OutputStream op = so.getOutputStream();
            DataOutputStream dop = new DataOutputStream(op);
            //客户端要把信息发出去，也要控制台敲内容
            Scanner sc = new Scanner(System.in);
            DataInputStream di = new DataInputStream(so.getInputStream());
            String info;//显示
            while (true) {
                System.out.println("我说:");
                info = sc.next();
                dop.writeUTF(info);
                if (info.equals("bye")) {
                    break;
                }
                //取到服务器端发回的信息
                String info2 = di.readUTF();
                System.out.println(id2 + "说:" + info2);
                if (info2.equals("bye")) {
                    break;
                }
            }
            di.close();
            dop.close();
        }
    }
}